import pandas as pd
import numpy as np
from loop import *
dir = r"D:\data_cmp\airfoil_csv"

def check_seq(file, isequal):
    '''
        检查是否为逆时针顺序
    :param file: 文件名
           isequal: True表示允许相等; False表示不允许相等
    :return: True/False
    '''
    df = pd.read_csv(file)
    x = df['x']
    y = df['y']

    i = 0
    n = x.shape[0]
    if isequal is False:
        while (i < n - 1 and x[i] > x[i + 1]):
            i += 1
        while (i < n - 1 and x[i] < x[i + 1]):
            i += 1
    else:
        while (i < n - 1 and x[i] >= x[i + 1]):
            i += 1
        while (i < n - 1 and x[i] <= x[i + 1]):
            i += 1

    if i == n - 1:
        return True
    else:
        return False


def check_leading_trailing_v2(file):
    '''
        检查翼型前缘是否为1个点和后缘是否为2个点
    :return:
    '''
    df = pd.read_csv(file)
    x = df['x'].to_numpy()
    y = df['y'].to_numpy()

    def fmin(x):
        return np.argwhere(x == np.min(x)).flatten()

    # def fmax(x):
    #     return np.argwhere(x == np.max(x)).flatten()

    # 前缘(1个点)
    leading = fmin(x.flatten())
    # print("leading:", leading, x[leading], end="; ")

    # 后缘(2个点)
    trailing = []
    trailing.append(np.argmax(x.flatten()[0:leading[0]]))
    trailing.append(np.argmax(x.flatten()[leading[0]+1::]))
    trailing = np.asarray(trailing)
    # print("trailing:", trailing, x[trailing])

    return ((leading.shape == 1, x[leading] == 0, y[leading] == 0, x[leading], y[leading], leading),
            (trailing.shape[0] == 2, x[trailing] == 1, y[trailing] == 0, x[trailing], y[trailing], trailing))

def check_leading_trailing(file):
    '''
        检查翼型前缘是否为1个点和后缘是否为2个点
    :return:
    '''
    df = pd.read_csv(file)
    x = df['x'].to_numpy()
    y = df['y'].to_numpy()

    def fmin(x):
        return np.argwhere(x == np.min(x)).flatten()

    def fmax(x):
        return np.argwhere(x == np.max(x)).flatten()

    # 前缘(1个点)
    leading = fmin(x.flatten())
    # print("leading:", leading, x[leading], end="; ")

    # 后缘(2个点)
    trailing = fmax(x.flatten())
    # print("trailing:", trailing, x[trailing])

    return ((leading.shape == 1, x[leading] == 0, y[leading] == 0, x[leading], y[leading], leading),
            (trailing.shape[0] == 2, x[trailing] == 1, y[trailing] == 0, x[trailing], y[trailing], trailing))


def check_abn(dir, file):
    ordinates = pd.read_csv(os.path.join(dir, file), index_col=None, header=0).to_numpy()



    def isdd(arr):
        '''
            检测单调递减性
        :return:
        '''
        for i in range(arr.shape[0] - 1):
            if arr[i, 1] < arr[i+1, 1]:
                return False
        return True

    flag = isdd(ordinates[60:70])

    return flag

# if __name__ == "__main__":
    # res = check_seq(r"D:\data_cmp\airfoil_csv\BACNLF.csv")
    # print(res)
    # res = check_seq(r"D:\data_cmp\airfoil_csv\s1221.csv")
    # print(res)
    # check_leading_trailing("s1221.csv")

    # loop_dataset_prt(check_leading_trailing)

    # test
    # dir = r"D:\data_cmp\airfoil_csv_op\NACA4_950"
    # for file in os.listdir(dir):
    #     if not check_abn(dir, file):
    #         print(file[:-4])

